package com.sunshine.student_register.ui.fragment.note;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sunshine.student_register.database.StudentRegisterRepository;
import com.sunshine.student_register.database.register.RegisterEntity;

import java.util.List;

/**
 * @author sunshine
 * @date 2020/5/30
 * 查询当前用户所有签到记录的viewModel类
 */
public class NoteViewModel extends AndroidViewModel {

    private SharedPreferences sp;
    private StudentRegisterRepository repository;
    private MutableLiveData<List<RegisterEntity>> registerMutableLiveData;

    public NoteViewModel(@NonNull Application application) {
        super(application);

        registerMutableLiveData = new MutableLiveData<>();
        registerMutableLiveData.setValue(null);
        sp = getApplication().getSharedPreferences("app_setting", Context.MODE_PRIVATE);
        repository = new StudentRegisterRepository(getApplication());
    }

    MutableLiveData getAllRegister(){
        int userId = sp.getInt("user_id", -1);
        registerMutableLiveData.setValue(repository.getThisUserAllRegister(userId));
        return registerMutableLiveData;
    }
}
